/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-7-10
*
\*********************************************************************/

#include <iostream>
#include <QApplication>
#include <thread>
#include <unistd.h>
#include "debug.h"
#include "CamControl.h"
#include "DisplayControl.h"
#include "CamGainControl.h"
#include "ProtocolMCU.h"
#include "TerminalControl.h"
#include "FunctionTransfer.h"

#include "FocusControl.h"

static void TerminalControl(int agrc)
{
    if(agrc>1)
    {
        return;
    }
//    printf("<<<<<<<<<<<<<<<<<<<<***********************************>>>>>>>>>>>>>>>>>>>>>>\n");
//    printf("<<<<<<<<<<<<<<<<<<<<******                        *****>>>>>>>>>>>>>>>>>>>>>>\n");
//    printf("<<<<<<<<<<<<<<<<<<<<******  debug for camera 1.0  *****>>>>>>>>>>>>>>>>>>>>>>\n");
//    printf("<<<<<<<<<<<<<<<<<<<<******                        *****>>>>>>>>>>>>>>>>>>>>>>\n");
//    printf("<<<<<<<<<<<<<<<<<<<<***********************************>>>>>>>>>>>>>>>>>>>>>>\n");

    while(true)
    {
        printf("select a number:\n\n");
        printf("  1. information display.\n");
        printf("  2. device control. \n");
        printf("  3. other command. \n");

        int back = 0;  //用于返回上一层 跳出循环
        int ss = 0;
        int order = 0;
        std::cin >> order;
        printf("receive order %d.\n", order);
        switch(order)
        {
        //打印信息处理
        case 1:
            printf("information display.\n\n");
            //消息列表  普通信息 飞控信息 吊舱信息 (trans为吊舱)
            while(true)
            {
                printf("select a number:\n\n");
                printf("1. ask-answer information display.\n");
                printf("2. realtime information display.\n");
                printf("3. function information display. \n");
                printf("4. cancel all information display. \n");
                printf("5. return last layer. \n");

                int rev;
                std::cin >> rev;
                printf("receive order %d.\n", rev);

                switch(rev)
                {
                //打印信息处理
                case 1:
                    m_Debug.DEBUG_FOR_ANSER = 1;
                    break;
                case 2:
                    m_Debug.DEBUG_FOR_REAL = 1;
                    break;
                case 3:
                    m_Debug.DEBUG_FOR_FUNCTION = 1;
                    break;
                case 4:
                    m_Debug.DEBUG_FOR_ANSER = 0;
                    m_Debug.DEBUG_FOR_REAL = 0;
                    m_Debug.DEBUG_FOR_FUNCTION = 0;
                    break;
                case 5:
                    back = 1;
                    break;
                default:
                    printf("ilegal command ! \n");
                    break;
                }
                if(back == 1)
                {
                    back = 0;
                    break;
                }
            }
            break;
        //控制设备
        case 2:
            printf("device control. \n");
            while(true)
            {
                printf("select a number:\n\n");
                printf("1. increase gain.\n");
                printf("2. decrease gain.\n");
                printf("3. get gain.\n");
                printf("4. set gain.\n");
                printf("5. increase focus. \n");
                printf("6. decrease focus. \n");
                printf("7. get focus. \n");
                printf("8. set focus. \n");
                printf("9. increase contrast.\n");
                printf("10. decrease contrast.\n");
                printf("11. get contrast.\n");
                printf("12. set contrast.\n");
                printf("13. self check.\n");
                printf("14. back last layer. \n");
                printf("15. stop work. \n");
                int rev;
                std::cin >> rev;
                printf("receive order %d.\n", rev);

                switch(rev)
                {
                //控制命令
                case 1:
                    printf("1. increase gain.\n");
                    printf("current gain is %d.\n",CamGainControl::GetInstance().GetGain());
                    CamGainControl::GetInstance().IncGain();
                    printf("by increased, now current gain is %d.\n",CamGainControl::GetInstance().GetGain());
                    break;
                case 2:
                    printf("2. decrease gain.\n");
                    printf("current gain is %d.\n",CamGainControl::GetInstance().GetGain());
                    CamGainControl::GetInstance().DecGain();
                    printf("by decreased, now current gain is %d.\n",CamGainControl::GetInstance().GetGain());
                    break;
                case 3:
                    printf("3. get gain.\n");
                    printf("current gain is %d.\n",CamGainControl::GetInstance().GetGain());
                    break;
                case 4:
                    printf("4. set gain.\n");
                    printf("current gain is %d.\n",CamGainControl::GetInstance().GetGain());
                    printf("please enter a gain: it must be 0-100!\n");
                    std::cin >> ss;
                    printf("I will set the gain to %d\n",ss);
                    CamGainControl::GetInstance().SetGain(ss);
                    printf("by set gain, now current gain is %d.\n",CamGainControl::GetInstance().GetGain());
                    break;
                case 5:
                    printf("increase focus. \n");
                    printf("current focus is %d.\n",focusCtl.GetFocus());
                    printf("please enter the focus increase step: it must be 1-10!\n");
                    std::cin >> ss;
                    printf("I will increase focus %d\n",ss);                   
                    focusCtl.IncFocus(ss);
                    usleep(1000*1000);
                    printf("by increase focus, now current focus is %d.\n",focusCtl.GetFocus());
                    break;
                case 6:
                    printf("decrease focus. \n");
                    printf("current focus is %d.\n",focusCtl.GetFocus());
                    printf("please enter the focus decrease step: it must be 1-10!\n");
                    std::cin >> ss;
                    printf("I will decrease focus %d\n",ss);
                    focusCtl.DecFocus(ss);
                    usleep(1000*1000);
                    printf("by decrease focus, now current focus is %d.\n",focusCtl.GetFocus());
                    break;
                case 7:
                    printf("get focus. \n");
                    //focusCtl.GetFocusCommand();
                    usleep(50*1000);
                    printf("current focus is %d.\n",focusCtl.GetFocus());
                    usleep(1000*1000);
                    break;
                case 8:
                    printf("set focus. \n");
                    printf("current focus is %d.\n",focusCtl.GetFocus());
                    printf("please enter a focus: it must be 50-150!\n");
                    std::cin >> ss;
                    printf("I will set the focus to %d\n",ss);
                    focusCtl.SetFocus(ss);
                    usleep(3000*1000);
                    printf("by set focus, now current focus is %d.\n",focusCtl.GetFocus());
                    break;
                case 9:
                    printf("increase contrast.\n");
                    IncContrast();
                    usleep(100*1000);
                    printf("done!\n");
                    break;
                case 10:
                    printf("decrease contrast.\n");
                    DecContrast();
                    usleep(100*1000);
                    printf("done!\n");
                    break;
                case 11:
                    printf("get contrast. \n");
                    printf("current count is %d.\n",GetContrast());
                    usleep(1000*1000);
                    break;
                case 12:
                    printf("set contrast. \n");
                    printf("current contrast is %d.\n",GetContrast());
                    printf("please enter a contrast: it must be 50-150!\n");
                    std::cin >> ss;
                    printf("I will set the contrast to %d\n",ss);
                    SetContrast(ss);
                    usleep(3000*1000);
                    printf("by set contrast, now current contrast is %d.\n",GetContrast());
                    break;
                case 13:
                    printf("self check. \n");
                    focusCtl.MotoSelfCheck();
                    break;
                case 14:
                    printf("back last layer. \n");
                    back = 1;
                    break;
                case 15:
                    printf("stop work. \n");
                    focusCtl.SetWorkMode(false);
                    usleep(1000*1000);
                    printf("done!\n");
                    break;
                default:
                    printf("ilegal command ! \n");
                    break;
                }
                if(back == 1)
                {
                    back = 0;
                    break;
                }
            }
            break;
        case 3:
            while(true)
            {
                printf("other command. \n");
                printf("1. SetExposure \n");
                printf("2. SetFrameRate \n");
                printf("3. Set analog gain \n");
                printf("4. back to last layer \n");

                int rev;
                std::cin >> rev;
                printf("receive order %d.\n", rev);
                switch(rev)
                {
                //其他命令
                case 1:
                    printf("SetExposure ... input a data(unit is us):\n");
                    int data1;
                    std::cin>>data1;
                    //CGigeVison::GetInstance()->SetExposure(data1);
                    printf("done! \n");
                    break;
                case 2:
                    printf("SetFrameRate ... input a data(unit is frame):\n");
                    float data2;
                    std::cin>>data2;
                    //CGigeVison::GetInstance()->SetFrameRate(data2);
                    printf("done! \n");
                    break;
                case 3:
                    printf("Set analog gain ... input a data(unit is dB):\n");
                    float data3;
                    std::cin>>data3;
                    //CGigeVison::GetInstance()->SetAnalogGain(data3);
                    printf("done! \n");
                    break;
                case 4:
                    back = 1;
                    break;
                default:
                    printf("ilegal command!\n");
                    break;
                }
                if(back == 1)
                {
                    back = 0;
                    break;
                }
            }

            break;

        default:
            printf("ilegal command ! \n");
            break;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}


void TerminalCtlThread(int argc)
{
    std::thread(TerminalControl,argc).detach();
}

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/









